<?php

include 'Models.php';

$returnUrl = WebHelper::GetString("returnUrl");


if(WebHelper::IsPostKeyNotNull("action"))
{
    $action = WebHelper::PostString("action");
    
    DbConnector::Connect();

    if($action == "login")
    {        
        $email = WebHelper::PostString("email");
        $password = WebHelper::PostString("password");               
        
        if(Member::Login($email,$password))
            WebHelper::Redirect($returnUrl);
        else
            echo("Invalid Email or Password.");
    }
    else if($action == "register")
    {
        $member = new Member();
        
        $member->firstname = WebHelper::PostString("firstname");
        $member->lastname = WebHelper::PostString("lastname");
        $member->email = WebHelper::PostString("email");
        $member->password = WebHelper::PostString("password");
        
        $member->Save();
                
        Member::Login($member->email, $member->password);
        
        WebHelper::Redirect($returnUrl);
    }
    
    DbConnector::Disconnect();
}


?>

<html>
    <body>
        <div>
            <div>
                <h1>LOGIN</h1>
                <form method="post" action="register.php?returnUrl=<?= $returnUrl ?>">
                    <input type="hidden" id="action" name="action" value="login" />
                    Email: <input type="TEXT" id="email" name="email" />
                    Password: <input type="TEXT" id="password" name="password" />
                    <input type="SUBMIT" value="Login" />
                </form>
            </div>
            <div>
                <h1>REGISTER</h1>
                <form method="post" action="register.php?returnUrl=<?= $returnUrl ?>">
                    <input type="hidden" id="action" name="action" value="register" />
                    First Name: <input type="TEXT" id="firstname" name="firstname" />
                    Last Name: <input type="TEXT" id="lastname" name="lastname" />
                    Email: <input type="TEXT" id="email" name="email" />
                    Password: <input type="TEXT" id="password" name="password" />
                    Confirm Password: <input type="TEXT" id="confirmpassword" name="confirmpassword" />
                    <input type="SUBMIT" value="Login" />
                </form>
            </div>
        </div>
    </body>
</html>